package com.foryang.springcloud.dao.impl;

import com.foryang.springcloud.dao.PaymentDao;

import com.foryang.springcloud.entity.Payment;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class PaymentDaoImpl implements PaymentDao {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public Payment getPaymentById(Long id) {
        Query query = new Query(Criteria.where("rid").is(id));
        return mongoTemplate.findOne(query, Payment.class,"payment");
    }

    @Override
    public List<Payment> findAll() {
        return mongoTemplate.findAll(Payment.class);
    }

    @Override
    public Payment create(Payment payment) {
        return mongoTemplate.save(payment);
    }

    @Override
    public Object updateById(Payment payment) {
        Query query = new Query(Criteria.where("rid").is(payment.getRid()));
        Update update = new Update();
        update.set("rid", payment.getRid());
        update.set("serial", payment.getSerial());
        return mongoTemplate.updateFirst(query,update,Payment.class);
    }

    @Override
    public Object deleteById(Long id) {
        Query query = new Query(Criteria.where("rid").is(id));
        return mongoTemplate.remove(query,"payment");
    }

}
